<?php
namespace app\controller;

use app\BaseController;
use think\facade\Request;
use think\facade\Validate;
use think\facade\Db;

class Report extends BaseController {
    public function index() {
    try {
        // 定义举报原因列表
        $reasons = [
            "头像或个人资料作假",
            "骚扰广告",
            "诈骗/托",
            "色情低俗",
            "恶意引流",
            "骚扰/不文明语言",
            "视频中涉黄/辱骂/骚扰",
            "微信号不存在",
            "诱导消费",
            "涉及未成年",
            "其他举报理由"
        ];

        return json([
            'code' => 200,
            'msg' => '获取成功',
            'data' => $reasons
        ]);
    } catch (\Exception $e) {
        return json([
            'code' => 500,
            'msg' => '获取失败：' . $e->getMessage()
        ]);
    }
}

    public function report() {
        $token = Request::header('Authorization');
        if (empty($token) || !preg_match('/^Bearer\s+(.+)$/i', $token, $matches)) {
            return json(['code' => 401, 'msg' => '未授权访问']);
        }
        $token = $matches[1];
        $user = Db::name('think_users')->where('token', $token)->find();
        if (!$user) {
            return json(['code' => 401, 'msg' => 'token无效']);
        }
        $validate = Validate::rule([
            'reported_id' => 'require|integer',
            'reason' => 'require|max:255',
            'details' => 'max:1000',
            'images' => 'array',
        ]);
        $params = Request::post();
        if (!$validate->check($params)) {
            return json(['code' => 400, 'msg' => $validate->getError()]);
        }
        try {
            $data = [
                'reporter_id' => $user["id"],
                'reported_id' => $params['reported_id'],
                'reason' => $params['reason'],
                'details' => $params['details'] ?? null,
                'status' => 0, // 默认未处理
                'images' => json_encode($params['images'] ?? []),
                'create_time' => date('Y-m-d H:i:s'),
                'update_time' => date('Y-m-d H:i:s'),
            ];
            Db::name('think_report')->insert($data);
            return json(['code' => 200, 'msg' => '举报成功']);
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => '举报失败：' . $e->getMessage()]);
        }
    }
}